package udp;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class UdpTest {

    @Test
    public void udpServeTest() throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] buf = new byte[1024];
        DatagramPacket packet =new DatagramPacket(buf,0,buf.length);
        socket.receive(packet);
        System.out.println("接收到的信息 = " + new String(buf,0, packet.getLength()));
    }

    @Test
    public void udpClientTest() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String str ="Hello World!";
        DatagramPacket packet =new DatagramPacket(str.getBytes(),0,str.length(),new InetSocketAddress("127.0.0.1",8888));
        socket.send(packet);
    }
}
